package com.zhht.htzx;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.JsonObject;
import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.state.State;
import com.kunminx.binding_recyclerview.adapter.BaseDataBindingAdapter;
import com.kunminx.binding_recyclerview.adapter.SimpleDataBindingAdapter;
import com.nsgf.library.data.bean.OrderListBean;
import com.nsgf.library.data.bean.ProductBean;
import com.nsgf.library.data.response.ResponseStatus;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.ui.page.BaseFragment;
import com.nsgf.library.ui.page.StateHolder;
import com.nsgf.library.utils.ToastUtils;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.zhht.htzx.adapter.OrderAdapter;
import com.zhht.htzx.adapter.OrderBusinessAdapter;
import com.zhht.htzx.databinding.FragmentBusinessGoodsListBinding;
import com.zhht.htzx.databinding.FragmentOrderListBinding;
import com.zhht.htzx.databinding.ItemBusinessManagerGoodsBinding;
import com.zhht.htzx.ui.GoodsBusinessListActivity;
import com.zhht.htzx.ui.OrderDetailActivity;

import java.util.ArrayList;
import java.util.List;

public class BusinessOrderFragment extends BaseFragment {


    public static BusinessOrderFragment newInstance(int type) {
        BusinessOrderFragment orderFragment = new BusinessOrderFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        orderFragment.setArguments(bundle);
        return orderFragment;

    }

    private SimpleDataBindingAdapter<ProductBean, ItemBusinessManagerGoodsBinding> orderAdapter;
    private StateHolder mStates;
    private AccountRequester mAccountRequester;

    @Override
    protected void initViewModel() {
        mStates = getFragmentScopeViewModel(StateHolder.class);
        mAccountRequester = getFragmentScopeViewModel(AccountRequester.class);
    }


    private FragmentBusinessGoodsListBinding mBinding;

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        // 设置RecyclerView
        orderAdapter = new SimpleDataBindingAdapter<ProductBean, ItemBusinessManagerGoodsBinding>(getActivity(),
                R.layout.item_business_manager_goods, new DiffUtil.ItemCallback<ProductBean>() {
            @Override
            public boolean areItemsTheSame(@NonNull ProductBean oldItem, @NonNull ProductBean newItem) {
                return false;
            }

            @Override
            public boolean areContentsTheSame(@NonNull ProductBean oldItem, @NonNull ProductBean newItem) {
                return false;
            }
        }) {
            @Override
            protected void onBindItem(ItemBusinessManagerGoodsBinding binding, ProductBean item, RecyclerView.ViewHolder holder) {
                binding.setVm(item);
                binding.tvItemBusinessGoods.setText(1 == getType() ? "下架" : "上架");
                binding.tvItemBusinessGoods.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        JsonObject params = new JsonObject();
                        params.addProperty("status", getType() == 1 ? 0 : 1);
                        params.addProperty("id", item.getSpuId());
                        mAccountRequester.updateGoodsStatus(params);
                    }
                });
            }
        };
        return new DataBindingConfig(R.layout.fragment_business_goods_list, BR.vm, mStates)
                .addBindingParam(BR.adapter, orderAdapter);
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mBinding = (FragmentBusinessGoodsListBinding) getBinding();
        getLifecycle().addObserver(mAccountRequester);


        initListeners();
        mBinding.swipeRefreshLayout.autoRefresh();

        mAccountRequester.getProductBeanResult().observe(this, new Observer<ResponseStatus<List<ProductBean>>>() {
            @Override
            public void onChanged(ResponseStatus<List<ProductBean>> orderListBeanResponseStatus) {

                if (page == 1) {
                    mBinding.swipeRefreshLayout.finishRefresh();
                    mStates.goodsList.set(orderListBeanResponseStatus.getData());
                    updateEmptyView(mStates.goodsList.get().isEmpty());
                } else {
                    mBinding.swipeRefreshLayout.finishLoadMore();

                    List<ProductBean> currentList = mStates.goodsList.get();
                    if (currentList != null) {
                        currentList.addAll(orderListBeanResponseStatus.getData());
                        mStates.goodsList.set(currentList);
                    }
                }

            }
        });

        mAccountRequester.getSrcSuccess().observe(this, new Observer<ResponseStatus<String>>() {
            @Override
            public void onChanged(ResponseStatus<String> stringResponseStatus) {
                if (stringResponseStatus.getResponseCode() == 0)
                    mBinding.swipeRefreshLayout.autoRefresh();
            }
        });
    }

    private void updateEmptyView(boolean isEmpty) {
        if (isEmpty) {
            mBinding.tvEmpty.setVisibility(View.VISIBLE);
            mBinding.recyclerView.setVisibility(View.GONE);
        } else {
            mBinding.tvEmpty.setVisibility(View.GONE);
            mBinding.recyclerView.setVisibility(View.VISIBLE);
        }
    }

    private int getType() {

        return getArguments().getInt("type", 0);
    }

    private void initListeners() {
        // 下拉刷新
        mBinding.swipeRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

                page++;
                mAccountRequester.getTenantSpuList(getType());
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {

                page = 1;
                mAccountRequester.getTenantSpuList(getType());
            }
        });

    }

    private int page = 1;

}
